<?php
/**
 * 通过ajax+长轮询实现反向Ajax
 * @author webbc
 */
set_time_limit(0);//不设置请求超时时间
require('./conn.php');
$rec = $_COOKIE['username'];
$sql = "select * from kefu_msg where rec = '$rec' and isread = 0 limit 0,1";
while(true){
  $result = mysqli_query($conn,$sql);
  $msg = mysqli_fetch_assoc($result);
  //如果有客服人员的回复信息
  if(!empty($msg)){
    //把该信息置为已读
    $sql = "update kefu_msg set isread = 1 where mid = ".$msg['mid'];
    mysqli_query($conn,$sql);
    echo json_encode($msg);//返回json数据
    exit();//如果此次连接获取数据，就结束本次连接
  }
  sleep(1);//隔一秒循环1次
}